import csv
import re

#从package.csv中获取包的名称，存储在data里，用于进一步进行仓库的爬取，参数是filename，默认值是package.csv
def read_package_name(filename='package.csv'):
    # 从csv中获取包名
    #filename = 'package.csv'
    data = []
    with open(filename, encoding='utf-8', errors='ignore') as csvfile:
        csv_reader = csv.reader(csvfile)  # 使用csv.reader读取csvfile中的文件
        # header = next(csv_reader)        # 读取第一行每一列的标题
        for row in csv_reader:  # 将csv 文件中的数据保存到data中
            data.append(row[0])  # 选择某一列加入到data数组中
    return data


# 针对spec进行解析，主要是对字符串进行正则表达式的操作，将依赖项分开，存储到buildreq和req两个csv文件中
def read_spec(filename = 'spec.csv'):
    # filename = 'spec.csv'
    name = []
    Buildrequires = []
    Requires = []
    newBuildrequires = []
    newRequires = []

    with open(filename) as csvfile:
        reader = csv.reader(csvfile)
        for i in reader:
            name.append(i[0])
            Buildrequires.append(i[3])
            Requires.append(i[4])
    j = 0
    for i in Buildrequires:
        packagename = name[j]
        j += 1
        a = i.replace(" <= ", "<=").replace(" >= ", ">=").replace(" > ", ">").replace(" = ", "=").replace(" < ", "<")
        tmp_list = re.split("\s|,\s|,", a)  # 分隔字符串
        while "" in tmp_list:  # 判断是否有空值在列表中
            tmp_list.remove("")  # 如果有就直接通过remove删除
        while "%{name}=%{version}-%{release}" in tmp_list:  # 判断是否有在列表中
            tmp_list.remove("%{name}=%{version}-%{release}")  # 如果有就直接通过remove删除
        tmp_list = [item_ for item_ in tmp_list if ('%{' not in item_)]
        tmp_list.insert(0,packagename)
        newBuildrequires.append(tmp_list)

    j = 0
    for i in Requires:
        packagename = name[j]
        j += 1
        a = i.replace(" <= ", "<=").replace(" >= ", ">=").replace(" > ", ">").replace(" = ", "=").replace(" < ", "<")
        tmp_list = re.split("\s|,\s|,", a)  # 分隔字符串
        while "" in tmp_list:  # 判断是否有空值在列表中
            tmp_list.remove("")  # 如果有就直接通过remove删除
        while "%{name}=%{version}-%{release}" in tmp_list:  # 判断是否有在列表中
            tmp_list.remove("%{name}=%{version}-%{release}")  # 如果有就直接通过remove删除
        tmp_list = [item_ for item_ in tmp_list if ('%{' not in item_)]  # 去除一些错误依赖
        tmp_list.insert(0, packagename)
        newRequires.append(tmp_list)

    # 计数查看依赖项是否在已有软件包的里面的情况，ok是能找到相应的包，no是没有找到
    ok = 0
    no = 0
    for item in newBuildrequires:
        for i in item:
            if i in name:
                ok += 1
            else:
                no += 1
    for item in newRequires:
        for i in item:
            if i in name:
                ok += 1
            else:
                no += 1
    print(ok)
    print(no)


    # 写入csv中
    with open('buildrequire.csv', 'w', encoding='UTF8', newline='') as f:
        writer = csv.writer(f)
        # write the data
        writer.writerows(newBuildrequires)

    with open('require.csv', 'w', encoding='UTF8', newline='') as f:
        writer = csv.writer(f)
        # write the data
        writer.writerows(newRequires)

